package com.yizhuoyan.his.webapi.controller.registrar;

import com.yizhuoyan.his.ao.ModifyRegistrationLevelAo;
import com.yizhuoyan.his.ao.RegistrationLevelAo;
import com.yizhuoyan.his.dto.JSONResponse;
import com.yizhuoyan.his.entity.HosRegistrationLevelEntity;
import com.yizhuoyan.his.service.registrar.RegistrationLevelService;
import com.yizhuoyan.his.webapi.controller.registrar.vo.RegistrationLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class RegistrationLevelController {
    @Autowired
    RegistrationLevelService registrationLevelService;

    @PostMapping("/api/registration-Level/")
    public ResponseEntity<JSONResponse> addRegistrationLevel(RegistrationLevelAo ao)throws Exception{
        HosRegistrationLevelEntity hosRegistrationLevelEntity= registrationLevelService.addRegistrationLevel(ao);
        return ResponseEntity.ok().build();
    }
    @DeleteMapping("/api/registration-Level/{id}")
    public ResponseEntity<Void> deleteRegistrationLevel(@PathVariable Long id)throws Exception{
        registrationLevelService.removeRegistrationLevel(id);
        return ResponseEntity.ok().build();
    }
    @DeleteMapping("/api/registration-level/{id}")
    public ResponseEntity<Void> deleteRegistrationLevels(@PathVariable Long id)throws Exception{
        registrationLevelService.removeRegistrationLevels(id);
        return ResponseEntity.ok().build();
    }
    @PutMapping("/api/registration-Level/{id}")
    public ResponseEntity<Void> modifyRegistrationLevel(@PathVariable Long id, ModifyRegistrationLevelAo ao)throws Exception{
        registrationLevelService.modifyRegistrationLevel(id,ao);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/api/registration-Level/{id}")
    public ResponseEntity<RegistrationLevelVo> loadRegistrationLevel(@PathVariable Long id)throws Exception{
        HosRegistrationLevelEntity entity = registrationLevelService.queryRegistrationLevelById(id);
        return ResponseEntity.ok(RegistrationLevelVo.of(entity));
    }

    @GetMapping("/api/registration-Level")
    public ResponseEntity<List<RegistrationLevelVo>> loadRegistrationLevel(String key, String orderBy)throws Exception{
        List<HosRegistrationLevelEntity> result = registrationLevelService.queryRegistrationLevelAll(key,orderBy);
        return ResponseEntity.ok(
                result.stream().
                        map(RegistrationLevelVo::of)
                        .collect(Collectors.toList()));
    }
}
